import cn.tedu.pojo.AccountEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class StartATM {
    static Map<String, AccountEntity> accountList = new HashMap<>();
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        AccountEntity account1 = new AccountEntity();
        account1.setAccount("123456789987654321");
        account1.setPassword(123456);
        account1.setMoney(50000.00);
        accountList.put("123456789987654321",account1);

        AccountEntity account2 = new AccountEntity();
        account2.setAccount("987654321123456789");
        account2.setPassword(123456);
        account2.setMoney(60000.00);
        accountList.put("987654321123456789",account2);
        login();
    }

    private static void login() {
        System.out.println("请输入银行账户：");
        String account = scanner.next();
        if (accountList.containsKey(account)){
            checkpassword(account);
        }else {
            System.out.println("账户不存在！");
            login();
        }
    }

    /**
     * 验证密码
     * @param account
     */
    private static void checkpassword(String account) {
        System.out.println("请输入密码：");
        Integer password = scanner.nextInt();
        AccountEntity accountEntity = accountList.get(account);
        if(accountEntity.getPassword().equals(password)){
            menu(accountEntity);
        }else {
            System.out.println("密码错误！");
            checkpassword(account);
        }
    }

    private static void menu(AccountEntity accountEntity) {
        System.out.println("欢迎使用ATM");
        System.out.println("1.查询余额\t2.取款\t3.存款\t4.转账\t5.退出");
        System.out.println("请选择功能：");
        Integer i  = scanner.nextInt();
        switch (i){
            case 1:
                findmoney(accountEntity);
                break;
            case 2:
                getmoney(accountEntity);
                break;
            case 3:
                insertmoney(accountEntity);
                break;
            case 4:
                movemoney(accountEntity);
                break;
            case 5:
                System.out.println("退出成功！");
                login();
                break;

            default:
                System.out.println("输入功能不匹配");
                menu(accountEntity);
                break;
        }
    }

    private static void movemoney(AccountEntity startaccount) {
        System.out.println("请输入转账账号：");
        String endaccount = scanner.next();
        if (endaccount.equals(startaccount.getAccount())){
            System.out.println("不能给自己转账！");
            movemoney(startaccount);
        }
        if(accountList.containsKey(endaccount)){

            updatamoney(startaccount,endaccount);
        }else {
            System.out.println("账号不存在");
            movemoney(startaccount);
        }
    }

    private static void updatamoney(AccountEntity startaccount, String endaccount) {
        System.out.println("请输入转账金额：");
        Integer money = scanner.nextInt();
        if(startaccount.getMoney()>money){
            startaccount.setMoney(startaccount.getMoney()-money);
            accountList.put(startaccount.getAccount(),startaccount);

            AccountEntity endacocuntEntity = accountList.get(endaccount);
            endacocuntEntity.setMoney(endacocuntEntity.getMoney()+money);
            accountList.put(endaccount,endacocuntEntity);
            System.out.println("转账成功");
            menu(startaccount);
        }else {
            System.out.println("余额不足！");
            updatamoney(startaccount,endaccount);
        }
    }

    private static void insertmoney(AccountEntity accountEntity) {
        System.out.println("请输入存款金额：");
        Integer money = scanner.nextInt();
        if(money%100==0){
            accountEntity.setMoney(accountEntity.getMoney()+money);
            accountList.put(accountEntity.getAccount(),accountEntity);
            System.out.println("存款成功！");
            menu(accountEntity);
        }else {
            System.out.println("您的存款金额不合规！");
            insertmoney(accountEntity);
        }
    }

    private static void getmoney(AccountEntity accountEntity) {
        System.out.println("请输入取款金额：");
        Integer money = scanner.nextInt();
        if(accountEntity.getMoney()>money){
            if(money%100==0){
                accountEntity.setMoney(accountEntity.getMoney()-money);
                accountList.put(accountEntity.getAccount(),accountEntity);
                System.out.println("取款成功");
                menu(accountEntity);
            }else {
                System.out.println("取款金额必须为100的整数倍！");
                getmoney(accountEntity);
            }
        }else {
            System.out.println("您的余额不足！");
            getmoney(accountEntity);
        }

    }

    private static void findmoney(AccountEntity accountEntity) {
        System.out.println("您的余额为："+accountEntity.getMoney());
        menu(accountEntity);
    }
}
